package TUILayer;


import ControlLayer.*;
import ModelLayer.RealException;

public class SalesUI {
	
	private SalesCtr salesCtr;
	private SalesLineUI salesLineUI;
	private InputParser inputParser;
	
	private LoginInfo loginInfo; 
	
	public SalesUI(InputParser inputParser, LoginInfo loginInfo)
	{
		salesCtr = new SalesCtr();
		salesLineUI = new SalesLineUI(inputParser);
		this.inputParser = inputParser;
		this.loginInfo = loginInfo;
	}
	
	public void start() 
	{
		
		boolean exit = false;
		int choise = 0;
		do {
			try {
				switch(loginInfo.getLoggedEmployeePositionId())
				{
					case 1:
					{
						choise = inputParser.getInt("\f *** SaleMenu ***\n\n1. Show a sale information\n2. Create sales\n3. Update sales\n4. Delete sales\n5. Confirm offer\n6. Confirm payment\n7. List all offers\n8. List all orders\n9. Return\n\nMake your choise: ");
						switch (choise) {
						case 1:
							showInfo();
							break;
						case 2:
							createSale();
							break;
						case 3:
							updateSale();
							break;
						case 4:
							deleteSale();
							break;
						case 5:
							confirmOffer();
							break;
						case 6:
							confirmPayment();
							break;
						case 7:
							listAllOffer();
							break;
						case 8:
							listAllOrder();
							break;
						case 9:
							exit = true;
							break;
						}
						break;
					}
					case 2:
					{
						choise = inputParser.getInt("\f *** SaleMenu ***\n\n1. Show a sale information\n2. List all offers\n3. List all orders\n4. Return\n\nMake your choise: ");
						switch (choise) {
						case 1:
							showInfo();
							break;
						case 2:
							listAllOffer();
							break;
						case 3:
							listAllOrder();
							break;
						case 4:
							exit = true;
							break;
						}
						break;
					}
					case 3:
					{
						choise = inputParser.getInt("\f *** SaleMenu ***\n\n1. Show a sale information\n2. Create sales\n3. Update sales\n4. Delete sales\n5. Confirm offer\n6. List all offers\n7. List all orders\n8. Return\n\nMake your choise: ");
						switch (choise) {
						case 1:
							showInfo();
							break;
						case 2:
							createSale();
							break;
						case 3:
							updateSale();
							break;
						case 4:
							deleteSale();
							break;
						case 5:
							confirmOffer();
							break;
						case 6:
							listAllOffer();
							break;
						case 7:
							listAllOrder();
							break;
						case 8:
							exit = true;
							break;
						}
						break;
					}
				}
			}
			catch (RealException e) {
			System.out.println("\n" + e.getError() + "Please try again");
			inputParser.printReturn();
			}
		} while (!exit);
	}
	
	// show a sale information
	public void showInfo() throws RealException
	{
		int id = inputParser
				.getInt("\f ***ShowInfoMenu ***\n\nPlease input the id of the sale: ");
		System.out.println("\f ***ShowInfoMenu ***\n");
		System.out.println(salesCtr.printSales(id));
		inputParser.printReturn();
	}
	
	// create an offer and add it to salesRecords
	public void createSale()
	{
		
		int customerId = inputParser
				.getInt("\f ***CreateSaleMenu ***\n\nPlease input the id of the customer: ");
		int employeeId = loginInfo.getLoggedEmployeeId();
		long saleStartDate = inputParser.getCurrentDate();
		try{
			int id = salesCtr.createOffer(customerId, employeeId, saleStartDate);
			System.out.println("\nThe new offer id: " + id);
			// Is the customer wants to pick up the products?
			int select = 0;
			
			select = inputParser
						.getInt("\f\n Would you like to pick up the product yourself?\n1. Yes\n2. No\nPlease make your choice: ");
			switch (select) {
				case 1:
					salesCtr.hasPickup(id);// customer can get pick up discount
					break;
				case 2:
					String address = inputParser.getString("\f\nPlease input the delivery address: ");
					salesCtr.noPickup(id, address);
					break;
				}
			
			// choose to continue to saleline menu or return
				salesLineUI.start(id);
		}
		catch(RealException e)
		{
			System.out.println("\n" + e.getError());
			inputParser.printReturn();
		}
		
	}
	
	// update a sale
	public void updateSale() throws RealException
	{
		int id = inputParser
				.getInt("\f ***UpdateSaleMenu ***\n\nPlease input the id of the sale: ");
		salesCtr.getSales(id);
		int choise = 0;
		String value = "";
		boolean exit = false;
		do {
			choise = inputParser
					.getInt("\f ***UpdateSaleMenu ***\n\n1. Edit employee\n2. Edit customer\n3. Edit start date\n4.  Edit salelines\n5. Return\n\nMake your choise: ");
			
			switch (choise) {
				case 1:
					value = String.valueOf(inputParser
						.getInt("\f ***UpdateSaleMenu ***\n\nPlease input the new employee id: "));
					break;
				case 2:
					value = String.valueOf(inputParser
						.getInt("\f ***UpdateSaleMenu ***\n\nPlease input the new customer id: "));
					break;
				case 3:
					value = String.valueOf(inputParser.getDate("\f ***UpdateSaleMenu ***\n\nPlease input the new date (dd/mm/yyyy) of the sale: "));
					break;
				case 4:
					salesLineUI.start(id);
					break;
				case 5:
					exit = true;
					break;
				}
			
			salesCtr.updateSales(id, choise, value);
			inputParser.printReturn();
		} while (!exit);
		
	}
	
	// delete a sale
	public void deleteSale() throws RealException
	{
		int id = inputParser
				.getInt("\f ***DeleteSaleMenu ***\n\nPlease input the id of the sale: ");
		salesCtr.removeSales(id);
		System.out.println("\nSale with id " + id + " has been deleted");
		inputParser.printReturn();
	}
	
	// change status from offer to order for a sale
	public void confirmOffer() throws RealException
	{
		int id = inputParser
				.getInt("\f ***ChangeStatusMenu ***\n\nPlease input the id of the sale: ");
		String statusBefore = salesCtr.getOffer(id).getStatus().toString();
		salesCtr.getOffer(id).confirmOffer();
		String statusAfter = salesCtr.getOrder(id).getStatus().toString();
		System.out.print("\nThe sale with id " + id + " has changed from status " + statusBefore + " to status " + statusAfter);
		
		inputParser.printReturn();
		
	}
	
	public void confirmPayment() throws RealException
	{
		int id = inputParser
				.getInt("\f ***ChangeStatusMenu ***\n\nPlease input the id of the sale: ");
		salesCtr.getOrder(id).confirmPayment();
		String payStatusAfter = salesCtr.getOrder(id).getPayStatus().toString();
		System.out.print("\nThe sale with id " + id + " is " + payStatusAfter);
		
		inputParser.printReturn();
	}
	
	// print all offers
	public void listAllOffer()
	{
		System.out.println("\f ***ListAllOfferMenu ***\n");
		System.out.println(salesCtr.listAllOffer());
		inputParser.printReturn();
	}
	
	// print all orders
	public void listAllOrder()
	{
		System.out.println("\f ***ListAllOrderMenu ***\n");
		System.out.println(salesCtr.listAllOrder());
		inputParser.printReturn();
	}
	
	
	
}
